--[[
 * 直接给友方全体加个buff   触发时机 42       效果参数里直接配 一个buffId
 *
 * 这个buff的效果就是 , 检测自己是否是生命最小的, 如果是,就加一个buff(减伤效果),并且移除原来生命值最小的那个人的那个减伤buff
]]
---@class BuffEffect300039000:BaseBuffEffect
BuffEffect300039000 = ClientFight.CreateClass("BuffEffect300039000", ClientFight.BaseBuffEffect)
local t = BuffEffect300039000
local BuffEffect300039000 = "BuffEffect300039000";
---@param fight Fight
---@param buff FighterBuff
function t:add(fight, buff)
    local add = ClientFight.BaseBuffEffect.add(self, fight, buff, false);
    t:action(fight, buff);
    return add;
end

---@param fight Fight
---@param buff FighterBuff
function t:action(fight, buff, ...)
    local parameters = {...};
    local team = fight.teams[buff.target.teamIndex];
    local minHp = team.paramObj[BuffEffect300039000];

    if (parameters == nil or table.getn(parameters) < 1) then
        --说明是开场 或 替补入场
        local newMinHp = t:getMinHp(fight, buff.target);
        if (newMinHp == nil) then
            return IBuffEffect.FAIL;
        end
        if (minHp ~= nil and newMinHp.fighterId == minHp.fighterId) then
            return IBuffEffect.FAIL;
        end
        minHp = newMinHp;

        team.paramObj[BuffEffect300039000] = minHp;
        local logMinHp = minHp;
        SkillRoleMgr.savePathFile("[" + fight.frame + "][EFFECT]Fighter " + buff.target.fighterId + " Buff:"
                + buff.modelId + " 给生命最小的人(" + logMinHp.fighterId + ")加个buff" + buff.buffBean.f_BuffEffectValue[1]);
        BuffManager.addBuff(fight, buff.skill, minHp, minHp, false, buff.buffBean.f_BuffEffectValue[1], 1);
        return IBuffEffect.FAIL;
    end
    local change = parameters[1];
    if (change < 0) then
        --减少,直接跟最小的人比就行了
        if (buff.target.hp >= minHp.hp) then
            return IBuffEffect.FAIL;
        end
    else
        --如果是加血, 自己不是那个生命最低的人就直接返回了
        if (minHp.fighterId ~= buff.target.fighterId) then
            return IBuffEffect.FAIL;
        end
    end
    team.paramObj[BuffEffect300039000] = buff.target;
    local logMinHp = minHp;
    --自己变成了生命最小的那个人,给自己加buff,把原来的人身上的buff移除
    SkillRoleMgr.savePathFile("[" + fight.frame + "][EFFECT]Fighter " + buff.target.fighterId + " Buff:"
            + buff.modelId + " 生命值最小的人已经发生变化了, 移除原来的人(" + logMinHp.fighterId + ")的buff " + buff.buffBean.f_BuffEffectValue[1]);

    BuffManager.removeFightBuff(fight, minHp, buff.buffBean.f_BuffEffectValue[1], true);

    SkillRoleMgr.savePathFile("[" + fight.frame + "][EFFECT]Fighter " + buff.target.fighterId + " Buff:"
            + buff.modelId + " 给生命最小的人(" + buff.target.fighterId + ")加个buff" + buff.buffBean.f_BuffEffectValue[1]);
    BuffManager.addBuff(fight, buff.skill, buff.target, buff.target, false, buff.buffBean.f_BuffEffectValue[1], 1);

    return IBuffEffect.FAIL;

end

function t:getMinHp(fight, target)
    local team = fight.teams[target.teamIndex];
    local minHp = nil;
    for fighter in pairs(team.fighters) do
        if (fighter:isSummons()) then
            goto continue;
        end
        if (fighter:isDie()) then
            goto continue;
        end
        if (minHp == nil) then
            minHp = fighter;
        else
            if (minHp.hp > fighter.hp) then
                minHp = fighter;
            end
        end
     ::continue::
    end
    return minHp;
end
t.New()